package org.rsbot.event.impl;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;

import org.rsbot.bot.Bot;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Methods;
import org.rsbot.script.wrappers.RSPlayer;

public class DrawPlayers implements PaintListener {

    Methods ctx;

    public DrawPlayers(Bot bot) {
        ctx = bot.getMethods();
    }

    public void onRepaint(final Graphics render) {
        if (!ctx.isLoggedIn()) {
            return;
        }
        
        final org.rsbot.client.RSPlayer[] players = Bot.getClient().getRSPlayerArray();
        if (players == null) {
            return;
        }
        
        final FontMetrics metrics = render.getFontMetrics();
        for (final org.rsbot.client.RSPlayer element : players) {
            if (element == null) {
                continue;
            }
            final RSPlayer player = new RSPlayer(element);
            final Point location = player.getScreenLocation();
            if (!ctx.pointOnScreen(location)) {
                continue;
            }
            render.setColor(Color.RED);
            render.fillRect((int) location.getX() - 1, (int) location.getY() - 1, 2, 2);
            String s = "Name : " + player.getName() + " - Lv : [ " + player.getCombatLevel() + " ]";
            render.setColor(player.isInCombat() ? Color.RED : player.isMoving() ? Color.ORANGE : Color.GREEN);
            render.drawString(s, location.x - metrics.stringWidth(s) / 2, location.y - metrics.getHeight() / 2);
            String msg = player.getMessage();
            boolean raised = false;

            render.setColor(Color.WHITE);
            if (player.getAnimation() != -1 || player.getGraphic() != -1) {
                s = "[Ani] : " + player.getAnimation() + " - [Graph] : " + player.getGraphic();
                render.drawString(s, location.x - metrics.stringWidth(s) / 2, location.y - metrics.getHeight() * 3 / 2);
				raised = true;
            }
			if (msg != null) {
				render.setColor(Color.BLUE);
				render.drawString(msg, location.x - metrics.stringWidth(msg) / 2, location.y - metrics.getHeight() * (raised ? 5 : 3) / 2);
			}
        }
    }
}
